<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>柯里化</title>
</head>
<body>
	<p>柯里化函数：将需要计算的值存储在闭包中，到需要计算的时候一次性计算得到结果，取代每一次的计算累积</p>
	<script type="text/javascript">
	// 柯里化简单实现
		+function (argument) {
			let total = (function() {
			let val = [];
			return function () {
				let money = 0;
				if (arguments.length === 0) {
					for (let i = 0, len = val.length; i < len; i++) {
						money += val[i];
					}
					return money;
				} else {
					Array.prototype.push.apply(val,arguments)
				}
			}
			})();
			total(100);
			total(100);
			total(100);
			console.log(total());
		}
	// 函数节流
		let throttle = function (fn, time) {
				let _self = fn,
					timer,
					firstTime = true;
				return function () {
					let _me = this;
					if (firstTime) {
						_self.apply(_me, arguments)
						return firstTime = false;
					}
					if (timer) {
						return false;
					}

					timer = setTimeout(() => {
						clearTimeout(timer);
						timer = null;
						_self.apply(_me, arguments)
					}, time || 500)
				}
			}
		window.onresize = throttle(function(){ 
						console.log( 1 );
		}, 500 );
	//函数限流
		let timeChunk = function (data, fn, count) {
			let dataChunck,
				timer;
			let len = data.length;
			let start = function () {
				for (let i = 0; i < Math.min(count || 1, len); i++) {
					dataChunck = data.shift();
					fn (dataChunck);
				}
			}
			return function () {
				timer = setInterval(() => {
					if (data.length === 0) {
						return clearInterval(timer);
					} else {
						start()
					}
				}, 200)
			}

		}

		class TestTimeChunck  {
			constructor (node, count) {
				this.node = node;
				this.count = count
			}
			arg () {
					let arg = [];
					for (var i = 0; i < this.node; i++) {
						[].push.call(arg, i)
					}
					return arg;
				}
			create (n) {
					let oDiv = document.createElement('div');
						oDiv.innnerHTML = n
						document.body.appendChild(oDiv);
			}
		}
		let tThuck = new TestTimeChunck(100, 10);
		timeChunk(tThuck.arg(), tThuck.create, tThuck.count)();

	</script>
</body>
</html>